package com.htn.common.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.util.Calendar.MONTH;

@Slf4j
public class DateUtil {

	public static final Long DAY = 24L * 60L * 60L * 1000L;

	/**
	 * 处理时间 getDateList Author：Yuan TF param
	 * 1、 int coursenum 本次课程计划总共有多少节课程
	 * 2、String strattime 第一次上课的开始时间
	 * 3、int betweenDays 中间间隔天数 例如：周循环间隔7天
	 * 4、int continuousDays 连续上课的天数例如：周循环上可天数就是1天 连续上课5天 或者任意几天
	 * 5、boolean interruptStatus 是否有中断的 如果interruptStatus数字等于1填写true 等于0填写FALSE
	 * 6、String interruptNumberof 中断的第一次用 逗号隔开 例如中断 3次和第5次 就是3,5
	 * date 2014年7月6日 下午12:55:28
	 */
	public static List<String> getDateList(int coursenum, String strattime, int betweenDays, int continuousDays,
										   boolean interruptStatus, String interruptNumberof) {
		if (betweenDays != 7) {
			betweenDays += 1;
		}
		int intnumlength = 0;
		String[] intnum = {};
		if (interruptNumberof != null && interruptNumberof != "" && interruptStatus) {
			interruptNumberof = interruptNumberof.replaceAll(",", "，");
			intnum = interruptNumberof.split("，");
			intnumlength = intnum.length;
		}
		List<String> datelist = new ArrayList<String>();
		// 时间格式或
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date1;
		try {
			date1 = bartDateFormat.parse(strattime);
			Calendar rightNow = Calendar.getInstance();
			// 连续上课天数
			int total = coursenum / continuousDays;
			int qy = coursenum % continuousDays;
			if (qy != 0) {
				total = total + 2 + intnumlength;
			} else {
				total = total + 1 + intnumlength;
			}
			for (int j = 1; j < total; j++) {
				boolean stutas = true;
				if (interruptStatus) {
					for (String intn : intnum) {
						int temp = 0;
						if (intn != null) {
							temp = Integer.parseInt(intn);
							if (temp == j) {
								rightNow.setTime(date1);
								int tepbetweenDays = betweenDays;
								if (betweenDays != 7) {
									tepbetweenDays = betweenDays - 1;
								}
								rightNow.add(Calendar.DATE, tepbetweenDays);
								date1 = rightNow.getTime();
								stutas = false;
							}
						}
					}
				}
				if (stutas) {
					if (qy != 0) {
						if (total - 1 == j) {
							continuousDays = qy;
						}
					}
					for (int i = 0; i < continuousDays; i++) {
						rightNow.setTime(date1);
						// 获取时间秒
						rightNow.add(Calendar.DATE, i);
						Date date2 = rightNow.getTime();
						datelist.add(bartDateFormat.format(date2));
					}
					date1 = rightNow.getTime();
					rightNow.setTime(date1);
					rightNow.add(Calendar.DATE, betweenDays);
					date1 = rightNow.getTime();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datelist;
	}


	/**
	 * @param date
	 * @param patten
	 * @return
	 */
	public static Date formatTimeToDate(String date, String patten) {
		if (patten == null) {
			patten = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		try {
			if (date == null)
				return null;
			else
				return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化时间字符串
	 * 中转单专用
	 *
	 * @return 2020-03-20 12:19:08
	 */
	public static Date formatTimeToDateTransfer(String date) {
		String patten = "yyyy-MM-dd HH:mm:ss";
		StringBuilder sb = new StringBuilder(date + "0000000000000000000");//构造一个StringBuilder对象
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		String fordate = "";
		try {
			if (date == null)
				return null;
			else {
				for (int i = 0; i < sb.length(); i++) {
					if (i == 4 || i == 7) {
						char ch = sb.charAt(i);
						if (ch == '-') {
							continue;
						} else {
							sb.insert(i, '-');//在指定的位置1，插入指定的字符串
						}
					}
					if (i == 10) {
						sb.insert(i, ' ');
					}
					if (i == 13 || i == 16) {
						char ch = sb.charAt(i);
						if (ch == ':') {
							continue;
						} else {
							sb.insert(i, ':');//在指定的位置1，插入指定的字符串
						}
					}
				}
			}
			return sdf.parse(sb.substring(0, 19).toString());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化时间字符串
	 *
	 * @param patten 时间格式，NULL为标准日期时间
	 * @return
	 */
	public static String formatDateToString(Date date, String patten) {
		if (date == null) {
			return "";
		}
		if (patten == null) {
			patten = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		return sdf.format(date);
	}

	/**
	 * 根据日历获取日期
	 *
	 * @param patten 时间格式，NULL为默认日期格式,differDay获取日期的第几天
	 * @return
	 */
	public static String getDateByCalendar(Date date, Integer differDay, String patten) {
		if (patten == null) {
			patten = "yyyy-MM-dd";
		}
		SimpleDateFormat sf = new SimpleDateFormat(patten);
		String nowDate = sf.format(date);
		// 通过日历获取日期
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sf.parse(nowDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.DAY_OF_YEAR, differDay);
		return sf.format(cal.getTime());
	}

	/**
	 * 判断时间是否在时间段内
	 *
	 * @param date         当前时间HH:mm:ss
	 * @param strDateBegin 开始时间 10:00:00
	 * @param strDateEnd   结束时间 11:00:00
	 * @return
	 */
	public static boolean isInDate(Date date, String strDateBegin,
								   String strDateEnd) {
		Date strDate = DateUtil.formatTimeToDate(strDateBegin, "HH:mm:ss");
		Date endDate = DateUtil.formatTimeToDate(strDateEnd, "HH:mm:ss");
		String dateString = DateUtil.formatDateToString(date, "HH:mm:ss");
		date = DateUtil.formatTimeToDate(dateString, "HH:mm:ss");
		if (date.getTime() >= strDate.getTime() && date.getTime() <= endDate.getTime()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回当前时间是周几  0为周日 6为周六
	 *
	 * @param date
	 * @return
	 */
	public static Integer getWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Integer w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		return w;
	}

	/**
	 * 获得几天前的时间
	 *
	 * @param toDay 当前时间
	 * @param agoX  几天前
	 * @return
	 */
	public static Date getAgoXDay(Date toDay, Long agoX) {
		return new Date(toDay.getTime() - (agoX * 86400000L));
	}

	/**
	 * 将当前时间加上几小时
	 *
	 * @param toDay  当前时间
	 * @param afterX 几小时
	 * @return
	 */
	public static Date getAfterXHour(Date toDay, Long afterX) {
		return new Date(toDay.getTime() + (afterX * 3600000L));
	}

	/**
	 * 获得毫秒对应的天数 小于1天 为小时 小于1小时 为分钟 小于分钟 为秒 否则返回空字符串
	 *
	 * @param ms
	 * @return
	 */
	public static String longTimeToDay(Long ms) {
		if (ms <= 0) {
			return "";
		}
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;
		Integer dd = hh * 24;
		Long day = ms / dd;
		Long hour = (ms - day * dd) / hh;
		Long minute = (ms - day * dd - hour * hh) / mi;
		Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
		Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
		if (day > 0) {
			return day + "天";
		} else if (hour > 0) {
			return hour + "小时";
		} else if (minute > 0) {
			return minute + "分钟";
		} else if (second > 0) {
			return second + "秒";
		} else {
			return "";
		}
		/*if (milliSecond > 0) {
			sb.append(milliSecond + "毫秒");
		}*/
	}


	/**
	 * 获得当前时间的上上个月最后
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getLast2MonthLastDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, -1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的上X个月的第一天
	 *
	 * @param now 当前时间
	 * @param X   前几个月
	 * @return
	 */
	public static Date getLastXMonthLastDay(Date now, Integer X) {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, X * -1);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的上个月最后
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getLastMonthLastDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的上个月最后
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getMonthLastDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return cale.getTime();
	}

	/**
	 * 获得当前月1号
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getMonthOneDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return cale.getTime();
	}

	/**
	 * 获得下个月1号
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getNextMonthOneDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的上上周最后一天
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getLast2WeekLastDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		int dayOfWeek = cale.get(Calendar.DAY_OF_WEEK) - 1;
		int offset1 = 7 - dayOfWeek;
		cale.add(Calendar.DATE, offset1 - 14);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的上周最后一天
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getLastWeekLastDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		int dayOfWeek = cale.get(Calendar.DAY_OF_WEEK) - 1;
		int offset1 = 7 - dayOfWeek;
		cale.add(Calendar.DATE, offset1 - 7);
		return cale.getTime();
	}

	/**
	 * 获得当前时间的本周第一天
	 *
	 * @param now 当前时间
	 * @return
	 */
	public static Date getWeekOneDay(Date now) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(now);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cale.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cale.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cale.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cale.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cale.add(Calendar.DATE, cale.getFirstDayOfWeek() - day);
		return cale.getTime();
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int subDataForDay(Date date1, Date date2) {
		int days = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
		return days;
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 *
	 * @param date1
	 * @param day
	 * @return
	 */
	public static Date dateSubDay(Date date1, Integer day) {
		return new Date((date1.getTime() - (day * 1000 * 3600 * 24)));
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 *
	 * @param date1
	 * @param day
	 * @return
	 */
	public static Date dateSubDay(String date1, Integer day) {
		return new Date((DateUtil.formatTimeToDate(date1, null).getTime() - (day * 1000 * 3600 * 24)));
	}

	/**
	 * 几个月后的时间
	 *
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date addMonth(Date date, int month) {
		Date parse = date;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}

	public static Date addDay(Date date, int day) {
		Date parse = date;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse);
		calendar.add(Calendar.DATE, day);
		return calendar.getTime();
	}

	public static Date beforeOneHourDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, -1);
		return calendar.getTime();
	}

	public static Time getDatePoor(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		String timeTemp = day * 24 + hour + ":" + min + ":00";
		if (timeTemp.length() < 2) {
			timeTemp = 0 + timeTemp;
		}
		return new Time(DateUtil.formatTimeToDate(timeTemp, "HH:mm:ss").getTime());
	}


	/**
	 * 获得当天x时x分x秒
	 *
	 * @return
	 */
	public static Date initDateByDay(Integer h, Integer m, Integer s) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, h);
		calendar.set(Calendar.MINUTE, m);
		calendar.set(Calendar.SECOND, s);
		return calendar.getTime();
	}


	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	/**
	 * 整数(秒数)转换为时分秒格式(xx:xx:xx)
	 *
	 * @param time
	 * @return
	 */
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	/**
	 * 根据出生年月计算生日
	 *
	 * @param birthday
	 * @return
	 * @throws Exception
	 */
	public static Integer getAge(String birthday) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date birth = null;
		try {
			birth = df.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar now = Calendar.getInstance();
		Calendar born = Calendar.getInstance();

		now.setTime(new Date());
		born.setTime(birth);

		if (born.after(now)) {
			throw new IllegalArgumentException("Can't be born in the future");
		}

		int age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
		if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
			age -= 1;
		}
		return age;
	}

	public static Integer daysBetween(Date early, Date late) {
		Calendar calst = Calendar.getInstance();
		Calendar caled = Calendar.getInstance();
		calst.setTime(early);
		caled.setTime(late);
		//设置时间为0时
		calst.set(Calendar.HOUR_OF_DAY, 0);
		calst.set(Calendar.MINUTE, 0);
		calst.set(Calendar.SECOND, 0);
		caled.set(Calendar.HOUR_OF_DAY, 0);
		caled.set(Calendar.MINUTE, 0);
		caled.set(Calendar.SECOND, 0);
		//得到两个日期相差的天数
		int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
				.getTime().getTime() / 1000)) / 3600 / 24;

		return days;
	}


	/**
	 * 用SimpleDateFormat计算时间差
	 *
	 * @throws ParseException
	 */
	public static String calculateTimeDifferenceBySimpleDateFormat(String smallDate, String bigDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date parse = null;
		Date date = null;
		try {
			parse = format.parse(smallDate);
			date = format.parse(bigDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long between = date.getTime() - parse.getTime();
		long day = between / (24 * 60 * 60 * 1000);
		long hour = (between / (60 * 60 * 1000) - day * 24);
		long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒");
		return hour + ":" + min + ":" + s;
	}

	//获取近30天每天的日期
	public static List<String> getNearlyThirtyDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String maxDateStr = sdf.format(date);
		String minDateStr = "";
		Calendar calc = Calendar.getInstance();
		List<String> nearlyThirtyDayList = new ArrayList();
		try {
			for (int i = 30; i >= 1; i--) {
				calc.setTime(sdf.parse(maxDateStr));
				calc.add(calc.DATE, -i);
				Date minDate = calc.getTime();
				minDateStr = sdf.format(minDate);
				nearlyThirtyDayList.add(minDateStr);
			}
		} catch (ParseException e) {
			log.error("错误信息[{}]", e.getMessage());
		}
		return nearlyThirtyDayList;
	}

	// 获取当天的开始时间
	public static Date getDayBegin() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	// 获取当天的结束时间
	public static Date getDayEnd() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	// 获取本周的开始时间
	public static Date getBeginDayOfWeek() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek += 7;
		}
		cal.add(Calendar.DATE, 2 - dayofweek);
		return getDayStartTime(cal.getTime());
	}

	// 获取本周的结束时间
	public static Date getEndDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginDayOfWeek());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		Date weekEndSta = cal.getTime();
		return getDayEndTime(weekEndSta);
	}

	/**
	 * 获取当月第一天的起始时间，例如2014-08-01 00:00:00
	 *
	 * @return 返回当月第一天的起始时间
	 */
	public static Date getMonthStartTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return getIntegralStartTime(cal.getTime());
	}

	/**
	 * 获取当月最后一天的结束时间，例如2014-08-31 23:59:59
	 *
	 * @return 返回当月最后一天的结束时间
	 */
	public static Date getMonthEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, getDayOfMonth(cal.getTime()));
		return getIntegralEndTime(cal.getTime());
	}

	/**
	 * 计算指定日期所在月份的天数
	 *
	 * @param date 指定日期
	 * @return 返回所在月份的天数
	 */
	public static int getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		if (date != null) {
			cal.setTime(date);
		}
		return cal.getActualMaximum(Calendar.DATE);
	}

	/**
	 * 返回指定日期的起始时间
	 *
	 * @param date 指定日期（例如2014-08-01）
	 * @return 返回起始时间（例如2014-08-01 00:00:00）
	 */
	public static Date getIntegralStartTime(Date date) {
		return getResetTime(date, 0, 0, 0);
	}

	/**
	 * 返回指定日期的结束时间
	 *
	 * @param date 指定日期（例如2014-08-01）
	 * @return 返回结束时间（例如2014-08-01 23:59:59）
	 */
	public static Date getIntegralEndTime(Date date) {
		return getResetTime(date, 23, 59, 59);
	}

	/**
	 * 获取重置指定日期的时分秒后的时间
	 *
	 * @param date   指定日期
	 * @param hour   指定小时
	 * @param minute 指定分钟
	 * @param second 指定秒
	 * @return 返回重置时分秒后的时间
	 */
	public static Date getResetTime(Date date, int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		if (date != null) {
			cal.setTime(date);
		}
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.SECOND, minute);
		cal.set(Calendar.MINUTE, second);
		return cal.getTime();
	}

	//获取某个日期的开始时间
	public static Timestamp getDayStartTime(Date d) {
		Calendar calendar = Calendar.getInstance();
		if (null != d) {
			calendar.setTime(d);
		}
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return new Timestamp(calendar.getTimeInMillis());
	}

	//获取某个日期的结束时间
	public static Timestamp getDayEndTime(Date d) {
		Calendar calendar = Calendar.getInstance();
		if (null != d) {
			calendar.setTime(d);
		}
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return new Timestamp(calendar.getTimeInMillis());
	}


	public static void main(String[] args) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date currentTime = df.parse("2004-03-26 13:31:40");   //当前系统时间
//		Date firstTime = df.parse("2004-01-02 11:30:24");     //查询的数据时间
//		String str = getBetweenTime(currentTime, firstTime);
//		System.out.println("获取的年月日时分秒时间差为：" + str);
		System.out.println(df.format(getLastWeekLastDay(new Date())));
	}

	//获取时间差方法
	public static String getBetweenTime(Date currentTime, Date firstTime) {
		long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
		Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型
		Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型
		int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
		int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
		int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
		if (day < 0) {
			month -= 1;
			currentTimes.add(Calendar.MONTH, -1);
			day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
		}
		if (month < 0) {
			month = (month + 12) % 12;//获取月
			year--;
		}
		long days = diff / (1000 * 60 * 60 * 24);
		long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); //获取时
		long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);  //获取分钟
		long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//获取秒
//		String CountTime = "" + "year" + "年" + month + "月" + day + "天" + hours + "小时" + minutes + "分" + s + "秒";
		String CountTime = hours + "小时" + minutes + "分" + s + "秒";
		return CountTime;
	}

	//Date类型转Calendar类型
	public static Calendar dataToCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * @Description: 获得两个日期之间的每天的日期集合
	 * @auther: qinjiwei
	 * @date: 2019/5/23
	 */
	public static  List<String> getDayBetween(String start, String end) {
		List<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date start_date = sdf.parse(start);
			Date end_date = sdf.parse(end);
			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start_date);
			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end_date);
			while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
				result.add(sdf.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
		} catch (ParseException e) {
			log.error("错误信息[{}]", e.getMessage());
		}
		return result;
	}

	/**
	 * 获取2个时间中间的随机值
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static Date randomDateBetween(String startTime, String endTime){
		Calendar calendar=Calendar.getInstance();
		Date startDate = formatTimeToDate(startTime, null);
		long min = startDate.getTime();
		Date endDate = formatTimeToDate(endTime, null);
		long max = endDate.getTime();
		//得到大于等于min小于max的double值
		double randomDate = Math.random()*(max-min)+min;
		//将double值舍入为整数，转化成long类型
		calendar.setTimeInMillis(Math.round(randomDate));
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		return sdf.format(calendar.getTime());
		return calendar.getTime();
	}

	/**
	 * 获取时间date1与date2相差的分钟数
	 *
	 * @param date1 起始时间
	 * @param date2 结束时间
	 * @return 返回相差的分钟数
	 */
	public static int getOffsetMinutes(Date date1, Date date2) {
		return getOffsetSeconds(date1, date2) / 60;
	}
	/**
	 * 获取时间date1与date2相差的秒数
	 *
	 * @param date1 起始时间
	 * @param date2 结束时间
	 * @return 返回相差的秒数
	 */
	public static int getOffsetSeconds(Date date1, Date date2) {
		return (int) ((date2.getTime() - date1.getTime()) / 1000);
	}
	/**
	 * 获取时间date1与date2相差的小时数
	 *
	 * @param date1 起始时间
	 * @param date2 结束时间
	 * @return 返回相差的小时数
	 */
	public static int getOffsetHours(Date date1, Date date2) {
		return getOffsetMinutes(date1, date2) / 60;
	}

	/**
	 * 获取时间date1与date2相差的天数数
	 *
	 * @param date1 起始时间
	 * @param date2 结束时间
	 * @return 返回相差的天数
	 */
	public static int getOffsetDays(Date date1, Date date2) {
		return getOffsetHours(date1, date2) / 24;
	}

	/**
	 * 获取时间date1与date2相差的周数
	 *
	 * @param date1 起始时间
	 * @param date2 结束时间
	 * @return 返回相差的周数
	 */
	public static int getOffsetWeeks(Date date1, Date date2) {
		return getOffsetDays(date1, date2) / 7;
	}

	/**
	 * 将分钟数转为小时并且精确到几小时几分钟
	 * @param liveTimeTotle
	 * @return
	 */
	public static String getTimeSecond(int liveTimeTotle){
		int hours = (int) Math.floor(liveTimeTotle / 60);
		int minute = liveTimeTotle % 60;
		return String.valueOf(hours).concat(".").concat(String.valueOf(minute));
	}
	/**
	 * 获取指定日期是星期几
	 *
	 * @param date 指定日期
	 * @return 返回星期几的描述
	 */
	public static String getWeekdayDesc(Date date) {
		final String[] weeks = new String[]{"星期天", "星期一", "星期二", "星期三", "星期四",
				"星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		if (date != null) {
			cal.setTime(date);
		}
		return weeks[cal.get(Calendar.DAY_OF_WEEK) - 1];
	}

	/**
	 * 获取上周开始时间
	 */
	public static Date getBeginDayOfLastWeek() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek += 7;
		}
		cal.add(Calendar.DATE, 2 - dayofweek - 7);
		return getDayStartTime(cal.getTime());
	}
	// 获取上周的结束时间
	public static Date getEndDayOfLastWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginDayOfLastWeek());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		Date weekEndSta = cal.getTime();
		return getDayEndTime(weekEndSta);
	}

	/**
	 * 获取上上周开始时间
	 */
	public static Date getBeginDayOfLastLastWeek() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek += 7;
		}
		cal.add(Calendar.DATE, 2 - dayofweek - 14);
		return getDayStartTime(cal.getTime());
	}
	// 获取上上周的结束时间
	public static Date getEndDayOfLastLastWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginDayOfLastLastWeek());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		Date weekEndSta = cal.getTime();
		return getDayEndTime(weekEndSta);
	}

	//获取第二天凌晨时间
	public static Long getNowToNextDaySeconds() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
	}


	/**
	 * date2比date1多的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date1,Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if(year1 != year2)   //同一年
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{
				if(i%4==0 && i%100!=0 || i%400==0)    //闰年
				{
					timeDistance += 366;
				}
				else    //不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2-day1) ;
		}
		else    //不同年
		{
			System.out.println("判断day2 - day1 : " + (day2-day1));
			return day2-day1;
		}
	}

}
